package com.ruoyi.training.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.Units;
import com.ruoyi.training.service.IUnitsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 单位Controller
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/units")
public class UnitsController extends BaseController
{
    @Autowired
    private IUnitsService unitsService;

    /**
     * 查询单位列表
     */
    @RequiresPermissions("training:units:list")
    @GetMapping("/list")
    public TableDataInfo list(Units units)
    {
        startPage();
        List<Units> list = unitsService.selectUnitsList(units);
        return getDataTable(list);
    }

    /**
     * 导出单位列表
     */
    @RequiresPermissions("training:units:export")
    @Log(title = "单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Units units)
    {
        List<Units> list = unitsService.selectUnitsList(units);
        ExcelUtil<Units> util = new ExcelUtil<Units>(Units.class);
        util.exportExcel(response, list, "单位数据");
    }

    /**
     * 获取单位详细信息
     */
    @RequiresPermissions("training:units:query")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") String unitId)
    {
        return success(unitsService.selectUnitsByUnitId(unitId));
    }

    /**
     * 新增单位
     */
    @RequiresPermissions("training:units:add")
    @Log(title = "单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Units units)
    {
        int insertStatus = unitsService.insertUnits(units);
        if (insertStatus == -1){
            return error("新增单位[" + units.getUnitName() +"]失败，该单位已存在");
        }
        return toAjax(insertStatus);
    }

    /**
     * 修改单位
     */
    @RequiresPermissions("training:units:edit")
    @Log(title = "单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Units units)
    {
        return toAjax(unitsService.updateUnits(units));
    }

    /**
     * 删除单位
     */
    @RequiresPermissions("training:units:remove")
    @Log(title = "单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable String[] unitIds)
    {
        return toAjax(unitsService.deleteUnitsByUnitIds(unitIds));
    }
}
